<template>
  <div class="edit-birthday">
    <!-- /选择时间 -->
    <van-datetime-picker
      v-model="currentDate"
      type="date"
      title="选择年月日"
      @confirm="sureModifyBirthday"
      @cancel="cancel"
      :min-date="minDate"
      :max-date="maxDate"
    />
  </div>
</template>

<script>
import dayjs from 'dayjs'
// 修改个人资料接口
import { modifyProfile } from '@/api/userInfo.js'
export default {
  name: 'EditUserName',
  data() {
    return {
      minDate: new Date(1970, 0, 1),
      maxDate: new Date(),
      currentDate: new Date(this.birthday),
      currentDateCopy: new Date(this.birthday)
    }
  },
  created() {},
  props: {
    birthday: {
      type: String
    }
  },
  methods: {
    async sureModifyBirthday() {
      try {
        const birthdayStr = dayjs(this.currentDate).format('YYYY-MM-DD')
        const data = {
          birthday: birthdayStr
        }
        await modifyProfile(data)
        this.$emit('close')
        this.$emit('sure', birthdayStr)
        this.$toast.success('修改成功')
      } catch (error) {
        this.$toast.fail('请稍后再试')
      }
    },
    cancel() {
      this.$emit('close')
      this.currentDate = this.currentDateCopy
    }
  },
  computed: {},
  watch: {},
  components: {}
}
</script>

<style lang="less" scoped>
.edit-birthday {
  margin-top: 50%;
}
</style>
